package com.tenement.action;

import com.tenement.domain.TenementPerformance;
import com.tenement.domain.TenementUser;
import com.tenement.service.PerformanceService;
import com.tenement.utils.PerformanceTotal;
import com.tenement.utils.Tools;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : PerformanceAction  //类名
 * @Description : 业绩中心Action  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-07-12 20:22  //时间
 */
@Controller
@RequestMapping("performanceAction")
public class PerformanceAction {

    @Resource
    PerformanceService performanceService;

    /**
     * 分页查询
     * @param performance
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("findAll")
    public Map<String,Object> findAll(TenementPerformance performance,Integer page,Integer limit){
        List<TenementPerformance> list=performanceService.findAll(performance,page,limit);
        Long overCount=performanceService.overCount(performance);
        PerformanceTotal performanceTotal=performanceService.selectTotal(performance);

        Map<String,Object> map=new HashMap<>();
        map.put("code",0); // 放入 code ，0
        map.put("msg", ""); // 放入msg “”
        map.put("count",overCount); // 放入总行数
        map.put("data", list);
        map.put("totalRow",performanceTotal);
        return map;
    }

    /**
     * 新增Or修改
     * @param performance
     * @return
     */
    @ResponseBody
    @RequestMapping("addOrUpdate")
    public int addOrUpdate(TenementPerformance performance, HttpServletRequest request){
        int row;
        TenementUser user= (TenementUser) request.getSession().getAttribute("user");
        if(StringUtils.isEmpty(performance.getPerformanceId())){
            row=performanceService.add(performance,user,request.getRemoteAddr());
        }else{
            row=performanceService.update(performance,user,request.getRemoteAddr());
        }
        return row;
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("findById")
    public TenementPerformance findById(String id){
        return performanceService.findById(id);
    }

    /**
     * 单个删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public int deleteById(String id,HttpServletRequest request){
        TenementUser user= (TenementUser) request.getSession().getAttribute("user");
        return performanceService.deleteById(id,request.getRemoteAddr(),user);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteAll")
    public int deleteAll(@RequestParam(value = "ids[]",required = false) String[] ids,HttpServletRequest request){
        TenementUser user= (TenementUser) request.getSession().getAttribute("user");
        return performanceService.deleteAll(ids,request.getRemoteAddr(),user);
    }


    /**
     * 日期格式化
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值

    }
}
